use std::process::Command;
use serde::{Deserialize, Serialize};
use super::env_path::get_full_path_from_script;

/// Helm版本信息响应结构
#[derive(Serialize, Deserialize)]
pub struct HelmVersionResponse {
    /// 执行是否成功
    pub success: bool,
    /// 版本信息或错误信息
    pub data: HelmVersionInfo,
    /// 错误信息（如果有）
    pub error: Option<String>,
}

/// Helm版本详细信息
#[derive(Serialize, Deserialize)]
pub struct HelmVersionInfo {
    /// Helm版本号
    pub version: String,
    /// Git提交ID
    pub git_commit: String,
    /// Git树状态
    pub git_tree_state: String,
    /// Go版本
    pub go_version: String,
}

/// 获取Helm软件的版本信息
///
/// 获取本机的Helm软件版本信息
///
/// # 参数列表
///
/// # 返回结果
/// * `HelmVersionResponse`  包含执行结果的结构体
#[tauri::command]
pub async fn get_helm_version() -> HelmVersionResponse {
    // 通过shell脚本获取完整的PATH环境变量
    let path = get_full_path_from_script();
    
    match Command::new("helm")
        .env("PATH", &path)
        .arg("version")
        .arg("--template")
        .arg("{{.Version}}\n{{.GitCommit}}\n{{.GitTreeState}}\n{{.GoVersion}}")
        .output() {
            Ok(output) => {
                if output.status.success() {
                    let version_output = String::from_utf8_lossy(&output.stdout).to_string();
                    let lines: Vec<&str> = version_output.lines().collect();
                    
                    if lines.len() >= 4 {
                        let version_info = HelmVersionInfo {
                            version: lines[0].to_string(),
                            git_commit: lines[1].to_string(),
                            git_tree_state: lines[2].to_string(),
                            go_version: lines[3].to_string(),
                        };
                        
                        HelmVersionResponse {
                            success: true,
                            data: version_info,
                            error: None,
                        }
                    } else {
                        HelmVersionResponse {
                            success: false,
                            data: HelmVersionInfo {
                                version: String::new(),
                                git_commit: String::new(),
                                git_tree_state: String::new(),
                                go_version: String::new(),
                            },
                            error: Some("版本信息格式不正确".to_string()),
                        }
                    }
                } else {
                    let error_info = String::from_utf8_lossy(&output.stderr).to_string();
                    HelmVersionResponse {
                        success: false,
                        data: HelmVersionInfo {
                            version: String::new(),
                            git_commit: String::new(),
                            git_tree_state: String::new(),
                            go_version: String::new(),
                        },
                        error: Some(error_info),
                    }
                }
            },
            Err(err) => {
                HelmVersionResponse {
                    success: false,
                    data: HelmVersionInfo {
                        version: String::new(),
                        git_commit: String::new(),
                        git_tree_state: String::new(),
                        go_version: String::new(),
                    },
                    error: Some(err.to_string()),
                }
            }
        }
}
